﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;

public partial class Admin_ModifyProductImg : ASPXBaseClass
{
    //上传商品图片基本物理目录    
    public string FILEPATH = HttpContext.Current.Server.MapPath("~/upload/product_img/");
    public string RELATIVEPATH = "/upload/product_img/";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"].ToString() != "admin")
        {
            Response.Redirect("~/Admin/RedirectLogin.htm");
        }
        if (!IsPostBack)
        {
            Bind();
        }
    }

    void Bind()
    {
        int Id = Convert.ToInt16(GZ.Common.HttpHelper.GetParameter("id", GZ.Common.HttpHelper.HttpType.GET));
        GZ.Entity.EProductInfo obj = new GZ.Entity.EProductInfo().GetProductInfoById(Id);
        this.lblProductName.Text = obj.ProductName;
        this.lblImgUrl1.Text = obj.ImgUrl1;
        this.lblImgUrl2.Text = obj.ImgUrl2;
        this.lblImgUrl3.Text = obj.ImgUrl3;
        this.lblImgUrl4.Text = obj.ImgUrl4;
        this.lblImgUrl5.Text = obj.ImgUrl5;
        this.lblImgUrl6.Text = obj.ShoppingCartThumbUrl;
    }

    /// <summary>
    /// 上传图片并验证
    /// </summary>
    void UploadImg(FileUpload fup, Label lbl)
    {
        //取得图片文件名
        string img_filename = fup.FileName.ToString().Trim();
        string extFileName = img_filename.Substring(img_filename.Length - 4);
        string BASEUrl = FILEPATH + this.lblProductName.Text;
        string RelativeUrl = RELATIVEPATH + this.lblProductName.Text + "/" + img_filename;
        //判断上传路径文件夹是否存在
        DirectoryInfo BASEDirectory = new DirectoryInfo(BASEUrl);
        if (!BASEDirectory.Exists)
        {
            Directory.CreateDirectory(BASEUrl);
        }

        //验证图片有效性 

        //保存图片
        if (!string.IsNullOrEmpty(img_filename))
        {
            fup.SaveAs(BASEUrl + "/" + img_filename);
        }
        lbl.Text = RelativeUrl;
        SaveImg();
    }

    /// <summary>
    /// 保存上传图片
    /// </summary>
    /// <returns>True未成功，Fasle为失败</returns>
    bool SaveImg()
    {
        GZ.Entity.EProductInfo obj = new GZ.Entity.EProductInfo();
        obj.Id = Convert.ToInt16(GZ.Common.HttpHelper.GetParameter("id", GZ.Common.HttpHelper.HttpType.GET));
        obj.ImgUrl1 = this.lblImgUrl1.Text;
        obj.ImgUrl2 = this.lblImgUrl2.Text;
        obj.ImgUrl3 = this.lblImgUrl3.Text;
        obj.ImgUrl4 = this.lblImgUrl4.Text;
        obj.ImgUrl5 = this.lblImgUrl5.Text;
        obj.ShoppingCartThumbUrl = this.lblImgUrl6.Text;
        return obj.UpdateProductImg(obj);
    }

    /// <summary>
    /// 上传图片1
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload1_Click(object sender, EventArgs e)
    {
        UploadImg(this.FileUploadImg1, this.lblImgUrl1);
    }

    /// <summary>
    /// 上传图片2
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload2_Click(object sender, EventArgs e)
    {
        UploadImg(this.FileUploadImg2, this.lblImgUrl2);
    }

    /// <summary>
    /// 上传图片3
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload3_Click(object sender, EventArgs e)
    {
        UploadImg(this.FileUploadImg3, this.lblImgUrl3);
    }

    /// <summary>
    /// 上传图片4
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload4_Click(object sender, EventArgs e)
    {
        UploadImg(this.FileUploadImg4, this.lblImgUrl4);
    }

    /// <summary>
    /// 上传图片5
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload5_Click(object sender, EventArgs e)
    {
        UploadImg(this.FileUploadImg5, this.lblImgUrl5);
    }

    /// <summary>
    /// 上传图片6（缩略图）
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload6_Click(object sender, EventArgs e)
    {
        UploadImg(this.FileUploadImg6, this.lblImgUrl6);
    }

    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
        int towidth = width;
        int toheight = height;

        int x = 0;
        int y = 0;
        int ow = originalImage.Width;
        int oh = originalImage.Height;
        #region 模式
        switch (mode)
        {
            case "HW"://指定高宽缩放（可能变形）                
                break;
            case "W"://指定宽，高按比例                    
                toheight = originalImage.Height * width / originalImage.Width;
                break;
            case "H"://指定高，宽按比例
                towidth = originalImage.Width * height / originalImage.Height;
                break;
            case "Cut"://指定高宽裁减（不变形）                
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y = 0;
                    x = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x = 0;
                    y = (originalImage.Height - oh) / 2;
                }
                break;
            default:
                break;
        }
        #endregion
        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
        //新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //清空画布并以透明背景色填充
        g.Clear(System.Drawing.Color.Transparent);
        //在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
            new System.Drawing.Rectangle(x, y, ow, oh),
            System.Drawing.GraphicsUnit.Pixel);
        try
        {
            //以jpg格式保存缩略图
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

}